package cn.xiaowenjie.services;

import cn.xiaowenjie.beans.Advert;
import cn.xiaowenjie.beans.CategoryDto;
import cn.xiaowenjie.helper.PageTransformer;
import cn.xiaowenjie.response.PageResult;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

import cn.xiaowenjie.beans.Category;
import cn.xiaowenjie.daos.CategoryDao;
import lombok.extern.slf4j.Slf4j;

import static cn.xiaowenjie.common.utils.CheckUtil.check;
import static cn.xiaowenjie.common.utils.CheckUtil.notNull;

/**
 * 配置业务处理类
 *
 * @author 肖文杰 https://github.com/xwjie/
 */
@Service
@Slf4j
public class CategoryService {

    @Autowired
    CategoryDao dao;

    public List<Category> getList() {
        log.info("getAll start ...");
        return (List<Category>) dao.findAll();
    }

    public PageResult<CategoryDto> getAll() {
        // 校验通过后打印重要的日志
        List<CategoryDto> categoryDtos = Lists.newArrayList();
        log.info("getAll start ...");
        List<Category> data = (List<Category>) dao.findAll();
        data.forEach(d -> {
            CategoryDto categoryDto = new CategoryDto();
            if (d.getParentId() > 0) {
                Category category = dao.findOne(d.getParentId());
                if (category != null) {
                    categoryDto.setParentId(category.getId());
                    categoryDto.setParentName(category.getName());
                }
            }
            categoryDto.setId(d.getId());
            categoryDto.setName(d.getName());
            categoryDtos.add(categoryDto);
        });
        log.info("getAll end, data size:" + data.size());
        PageInfo<CategoryDto> pageInfo = new PageInfo<>(categoryDtos);
        return PageTransformer.transform(pageInfo);
    }

    /**
     * 增加配置，需要管理员权限
     *
     * @param AdvertManager
     * @return
     */
    public long add(Category categoryAddDto) {
//        AdvertManager favoriteNew = dao.findByUserIdAndObjTypeAndObjId(userId, AdvertManager.getObjType(), AdvertManager.getObjId());
        Category favoriteNew = null;
        favoriteNew = dao.save(categoryAddDto);
        // 修改操作需要打印操作结果
        log.info("add AdvertManager success, id:" + favoriteNew.getId());
        return favoriteNew.getId();
    }

    /**
     * 根据id删除配置项
     * <p>
     * 管理员或者自己创建的才可以删除掉
     *
     * @param id
     * @return
     */
    public boolean delete(long id) {
        List<Category> categories = dao.findAllByParentId(id);
        categories.forEach(c->{
            System.out.println(c.getId());
            dao.delete(c.getId());
        });
        dao.delete(id);
        // 修改操作需要打印操作结果
        log.info("delete AdvertManager success, id:" + id);
        return true;
    }

    /**
     * 更新
     *
     * @param categoryAddDto
     * @return
     */
    public long update(Category categoryAddDto) {
        Category favoriteNew = null;
        favoriteNew = dao.save(categoryAddDto);
        // 修改操作需要打印操作结果
        log.info("add AdvertManager success, id:" + favoriteNew.getId());
        return favoriteNew.getId();
    }

    public List<Category> queryByParentId(Long parentId){
       return dao.findAllByParentId(parentId);
    }
}
